<?php
/**
 * Copyright 2013 Marek Kaszewski
 *
 * This file is part of the JHUB1 Online Community Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace Jhub1_Common\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Jhub1_Common\Model\SettingsObjects\AgentLocalConfigurationObject;
use Jhub1_Common\Model\SettingsObjects\AgentRemoteConfigurationObject;
use Jhub1_Common\Model\SettingsObjects\DateTimeObject;
use Jhub1_Common\Model\SettingsObjects\EndpointGeoLocalizationObject;

/**
 * @ORM\Entity
 * @ORM\Table(name="jhoEndpoints", indexes={@ORM\Index(name="uuid_index", columns={"id"})})
 */
class Endpoints extends POBase
{

    /**
     * @ORM\Column(type="string", name="name", length=50, nullable=false);
     * @var string
     * User friendly name - can be changed
     */
    private $name;

    /**
     * @ORM\Column(type="string", length=100, nullable=false)
     * @var string
     */
    private $icon;

    /**
     * @ORM\Column(type="string", length=100, nullable=false)
     * @var string
     */
    private $color;

    /**
     * @ORM\Column(type="string", name="description", length=120, nullable=true);
     * @var string
     * Created by user on the add endpoint page
     */
    private $description;

    /**
     * @ORM\Column(type="integer", name="sorder", nullable=false);
     * @var integer
     */
    private $sOrder;

    /**
     * @ORM\Column(type="integer", name="accesstype", nullable=false);
     * @var integer
     * Defines what type of access is available for the endpoint by third party entities
     * RW -0-
     * R -1-
     * W -3-
     */
    private $accessType;

    /**
     * @ORM\Column(type="integer", name="sharepolicy", nullable=false);
     * @var integer
     * Private -0- no sharing at all
     * Shared -1- te information is going ot be shared with selected users
     * Public -2- the endpoint becomes a part of public searchable repository
     */
    private $sharePolicy;

    /**
     * @ORM\Column(type="text", name="geolocalization",  nullable=true);
     * @var string
     * Endpoint localization for map visualization
     */
    private $geoLocalization;

    /**
     * @ORM\Column(type="text", nullable=true)
     * @var string
     */
    private $configValues;

    /**
     * @ORM\Column(type="string", name="address", nullable=true);
     * @var string
     */
    private $address;

    /**
     * @ORM\OneToOne(targetEntity="EndpointTypes")
     * @ORM\JoinColumn(name="type_id", referencedColumnName="id")
     */
    private $type;

    /**
     * @ORM\ManyToOne(targetEntity="Users")
     * @var Users
     * @ORM\JoinColumns({
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     * })
     */
    private $user;


    public function __construct(Users $user)
    {
        $this->setUser($user);
        $this->setOrder(0);
        $this->setCreated(new DateTimeObject());
    }

    /** @param Users $user
     *  @throws InvalidArgumentException
     */
    public function setUser(Users $user)
    {
        if ($user === null || $user instanceof Users) {
            $this->user = $user;
        } else {
            throw new InvalidArgumentException('$user must be instance of Users or null!');
        }
    }

    public function getIcon()
    {
        return $this->icon;
    }

    public function setIcon($val)
    {
        $this->icon = $val;
    }

    public function getColor()
    {
        return $this->color;
    }

    public function setColor($val)
    {
        $this->color = $val;
    }

    public function getName()
    {
        return $this->name;
    }

    public function setName($val)
    {
        $this->name = $val;
    }

    public function getDescription()
    {
        return $this->description;
    }

    public function setDescription($val)
    {
        $this->description = $val;
    }

    public function getOrder()
    {
        return $this->sOrder;
    }

    public function setOrder($val)
    {
        $this->sOrder = $val;
    }

    public function getSharePolicy()
    {
        return $this->sharePolicy;
    }

    public function setSharePolicy($val)
    {
        $this->sharePolicy = $val;
    }

    public function getAccessType()
    {
        return $this->accessType;
    }

    public function setAccessType($val)
    {
        $this->accessType = $val;
    }

    public function getGeoLocalization()
    {
        $geoloc = new EndpointGeoLocalizationObject();
        $geoloc->fromJSON($this->geoLocalization);
        return $geoloc;
    }

    public function setGeoLocalization(EndpointGeoLocalizationObject $geoLocObject)
    {
        $this->geoLocalization = $geoLocObject->toJSON();
    }

    public function getConfigValues()
    {
        $configuration = new AgentLocalConfigurationObject();
        $configuration->fromJSON($this->configValues);
        return $configuration;
    }

    public function setConfigValues(AgentLocalConfigurationObject $configurationObject)
    {
        $this->configValues = $configurationObject->toJSON();
    }

    public function getAddress()
    {
        return $this->address;
    }

    public function setAddress($val)
    {
        $this->address = $val;
    }

    public function toString()
    {
        $string           = '---------------------------------------------'. PHP_EOL;
        $string = $string . 'ENDPOINT OBJECT:                     ' . PHP_EOL;
        $string = $string . '---------------------------------------------'. PHP_EOL;
        $string = $string . 'ID:                         ' . $this->getId() . PHP_EOL;
        $string = $string . 'Created:                    ' . PHP_EOL . $this->getCreated()->toString() . PHP_EOL;
        $string = $string . 'Updated:                    ' . PHP_EOL . $this->getUpdated()->toString() . PHP_EOL;
        $string = $string . 'Build Id:                   ' . $this->buildId . PHP_EOL;
        $string = $string . 'Build Date:                 ' . PHP_EOL . $this->getBuildDate()->toString() . PHP_EOL;
        $string = $string . '---------------------------------------------'. PHP_EOL;
        $string = $string . 'OWNER:                      ' . PHP_EOL;
        $string = $string . $this->getUser()->toString() . PHP_EOL;
        return $string;
    }
}
